package com.pan.crawler.impl4;

import com.pan.crawler.Search;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;

import java.time.Duration;

public class SearchImpl4 implements Search {

    private final String searchUrl = "http://www.108shu.com/search/";

    private EdgeDriver edgeDriver;

    public SearchImpl4(){
        edgeDriver = new EdgeDriver();
        for (int i = 0; i < 3; i++) {
            try {
                    edgeDriver.navigate().to(searchUrl);
                    edgeDriver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
                    WebElement search = edgeDriver.findElement(By.id("searchkey"));
            }catch (Exception e){
                continue;
            }
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return;
        }
        edgeDriver.quit();
        throw new RuntimeException("www.108shu.com: 网站挂了");
    }

    @Override
    public void find(String content) {
        WebElement key = edgeDriver.findElement(By.id("searchkey"));
        key.sendKeys(content);
        key.submit();
    }

    @Override
    public void close() {
        edgeDriver.quit();
    }

//       "1.还挺好2.搜索要刷新一下\n3.搜索要等10秒广告自动关闭"
//    20240426貌似已经挂了

    public static void main(String[] args) {
        new SearchImpl4().find("斗罗");

    }
}
